package com.diver.flinkdemo.utils.xiaoyang;/**
 * @author yangs
 */


import com.alibaba.fastjson.JSONObject;
import com.diver.flinkdemo.utils.Md5Utils;
import org.apache.commons.lang3.StringUtils;

/**
 * 小杨平台http工具
 * @author yangs
 */
//@Slf4j
public class XiaoYangSignUtil {

    public static final String KEY = "KDXG";
    private static final String SECRET = "7KEY87EFC9356DOM73E50F280107CB0";

    private static StringBuilder string1(JSONObject params){
        StringBuilder sb = new StringBuilder();
        params.forEach((k, v) -> {
            if(!"KEY".equals(k) && !"TIMESTAMP".equals(k) && !"SIGN".equals(k)) {
                sb.append(k).append("=").append(v);
            }
        });
        return sb;
    }

    public static String sign(JSONObject params) {
        return sign(params, SECRET);
    }

    public static String sign(JSONObject params, String secret){

        StringBuilder sb = new StringBuilder();

        // string1
        StringBuilder sb1 = string1(params);

        // string2
        //long currentTimeStamp = DateUtil.nowSecondMolli();
        String timestamp = params.getString("TIMESTAMP");
        sb.append(secret).append(timestamp).append(sb1);

        return StringUtils.upperCase(Md5Utils.md5(sb.toString()));
    }


    public static void main(String[] args) {
        JSONObject jo = new JSONObject();
        /*jo.put("BAT", "54");
        jo.put("DEVICENUM", "865609040000881");
        jo.put("GPS", "3");
        jo.put("LA", "29.5497321q");
        jo.put("LO", "106.510284q");
        jo.put("LOCTIME", "2020-09-02 00:00:43");
        jo.put("POSDESC", "重庆天地");
        jo.put("TYPE", "1");
        jo.put("GSM", "5");*/
        System.out.println(System.currentTimeMillis());
        /*jo.put("DEVICENUM", "86560904004875");
        jo.put("TIMESTAMP", "1618390657");
        jo.put("SIGN", "862177041497812");
        jo.put("KEY", "KDXG");
        jo.put("AC1", "10:34-1-3-1111111");
        jo.put("AC2", "");
        jo.put("AC3", "");*/
        jo.put("DEVICENUM", "866104040230925");
        jo.put("TIMESTAMP", "1629942864");
        jo.put("KEY", KEY);
        String sign = sign(jo, SECRET);
        System.out.println(sign);
    }
}
